package com.example.f1.controller;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.example.f1.domain.Permit;
import com.example.f1.domain.Role;
import com.example.f1.service.PermitService;
import com.example.f1.service.RoleService;
import com.example.f1.service.UserService;
import org.aspectj.weaver.ast.Or;
import org.example.dayu521.OrRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class User {

    @Autowired
    private RoleService roleService;

    @Autowired
    private UserService userService;

    @Autowired
    private PermitService permitService;

    // 会话登录接口
    @RequestMapping("/doLogin")
    public SaResult doLogin(String name, String pwd) {
        if(name==null||pwd==null){
            return SaResult.error("请输入正确的登陆信息");
        }
        if(login(name,pwd)){
            return SaResult.ok("登录成功");
        }
        return SaResult.error("登录失败");

    }

    @RequestMapping(path = "/doLogin",consumes = "application/json")
    public SaResult doLogin(@RequestBody Map<String,String> param) {
        var user=param.get("user");
        var pwd=param.get("password");
        if(user==null||pwd==null){
            return SaResult.error("请输入正确的登陆信息");
        }

        if(login(user,pwd)){
            SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
            return SaResult.ok("登录成功").setData(tokenInfo.getTokenValue());
        }
        return SaResult.error("登录失败");

    }

    private boolean login(String user,String password){
        if(user==null||password==null){
            throw new IllegalArgumentException();
        }

        com.example.f1.domain.User db_user = userService.lambdaQuery().eq(com.example.f1.domain.User::getUsername, user)
                .eq(com.example.f1.domain.User::getPassword, password).one();

        if(db_user==null){
            return false;
        }

        StpUtil.login(db_user.getId());

        SaSession session = StpUtil.getSession();

        List<Role> role_list = roleService.lambdaQuery().select(Role::getRole_name)
                .inSql(Role::getId, "select r_id from t_user_role where u_id = " + db_user.getId())
                .list();
        session.set("role", role_list.stream().map(i->{
           return i.getRole_name();
        }).toList());

        var permit_list=permitService.get_permit(db_user.getId());

        session.set("permit",permit_list);

        return true;
    }

    @PostMapping("/logout")
    public SaResult logout(@RequestBody com.example.f1.domain.User us){
        StpUtil.logout();
        return SaResult.ok("注销成功");
    }

    @PostMapping("/create")
    public SaResult create_user(@RequestBody com.example.f1.domain.User us){
//        var user = new com.example.f1.domain.User();
        userService.save(us);
        return SaResult.ok("创建成功");
    }
}
